package com.key.service.impl;

import com.key.common.R;
import com.key.common.TranException;
import com.key.entity.AssetFieldLeft;
import com.key.entity.ModelLabel;
import com.key.entity.Photo;
import com.key.entity.PhotoLabel;
import com.key.mapper.AssetsMapper;
import com.key.mapper.LabelMapper;
import com.key.service.AssetCodeService;
import com.key.service.LabelService;
//import com.key.utils.CosUtils;
import com.key.utils.JbarcodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Service
public class LabelServiceImpl implements LabelService {
    @Autowired
    private LabelMapper labelMapper;

    @Override
    public R findAllLeft() {
        List<AssetFieldLeft> assetFieldLefts = labelMapper.selectAllLeft();
        R r=R.ok();
        r.setData(assetFieldLefts);
        return r;
    }

    @Override
    @Transactional
    public R addPhoto(List<Integer> ids) {
//        R r=new R();
//       // List<AssetFieldLeft> lefts=new ArrayList<>();
//        Integer i=0;
//        String s="";
//        for (Integer id : ids) {
//            AssetFieldLeft assetFieldLeft = labelMapper.selectLeftById(id);
//            String assetField = assetFieldLeft.getAssetField();
//            s+=assetField+System.lineSeparator();
//            i+=id;
//            //lefts.add(assetFieldLeft);
//        }
//        String idSum= i +String.valueOf(ids.size());
//        if (labelMapper.selectPhotoLabelByIdSum(idSum)==0){
//            MultipartFile file = JbarcodeUtil.createBarcode("test",s);
//            try {
//                URL url = CosUtils.uploadFile(file, "key-1316841246", CosUtils.formatKey(file));
//                Photo photo=new Photo();
//                photo.setUrl(url.toString());
//                photo.setType((byte) 0);
//                labelMapper.insertPhoto(photo);
//                Photo photo1 = labelMapper.selectPhotoByUrl(url.toString());
//                PhotoLabel photoLabel=new PhotoLabel();
//                photoLabel.setPhotoId(photo1.getId());
//                photoLabel.setIdSum(idSum);
//                for (Integer id : ids) {
//                    photoLabel.setAssetFieldId(id);
//                    labelMapper.insertPhotoLabel(photoLabel);
//                }
//
//                r= R.ok();
//            } catch (Exception e) {
//                throw new TranException("添加失败");
//            }
//        }else {
//            r.setCode(200);
//            r.setMsg("已存在");
//        }
        return null;

    }

    @Override
    @Transactional
    public R commitLabel(ModelLabel modelLabel) {
        try {
            int i1 = labelMapper.deleteModelLabel();
            int i = labelMapper.insertModelLabel(modelLabel);
            return i+i1>0?R.ok():R.no();
        }catch (Exception e){
            throw new TranException("提交失败");
        }

    }

    @Override
    public R findPhoto(Integer type) {
        R r=new R();
        List<Photo> photos = labelMapper.findPhotoByType(type);
        if (photos.size()>0){
            r=R.ok();
            r.setData(photos);
        }else {
            r.setCode(200);
            r.setMsg("无模板");
        }
        return r;
    }

    @Override
    public R deletePhotoLabel(Integer photoId) {
        int i = labelMapper.deletePhotoById(photoId);
        int i1 = labelMapper.deletePhotoLabelByPhotoId(photoId);

        return i+i1>0?R.ok():R.no();
    }

   /* @Override
    public R findModelLabelByPhotoId(Integer photoId) {
        R r=new R();
        ModelLabel modelLabel = labelMapper.selectModelLabelByPhotoId(photoId);
        if (modelLabel!=null){
            r=R.ok();
            r.setData(modelLabel);
        }else {
            r=R.no();

        }
        return r;

    }*/
}
